using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Users;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

namespace Code5Systems.FormMaster
{
	
	public partial class FormUserManager : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		
#region  Web Form Designer Generated Code
		
		//This call is required by the Web Form Designer.
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected System.Web.UI.HtmlControls.HtmlTable tblHeading;
		protected System.Web.UI.WebControls.Label lblHeading;
		protected System.Web.UI.WebControls.CheckBoxList cblUserRoles;
		protected System.Web.UI.WebControls.LinkButton cmdUpdate;
		protected CheckBox chkRegisterUser;
		
		protected DropDownList lstUserNameField;
		protected DropDownList lstPasswordField;
		protected DropDownList lstFirstNameField;
		protected DropDownList lstLastNameField;
		protected DropDownList lstEmailField;
		
		protected RequiredFieldValidator rfvUserNameField;
		protected RequiredFieldValidator rfvPasswordField;
		protected RequiredFieldValidator rfvFirstNameField;
		protected RequiredFieldValidator rfvLastNameField;
		protected RequiredFieldValidator rfvEmailField;
		
		//NOTE: The following placeholder declaration is required by the Web Form Designer.
		//Do not delete or move it.
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: This method call is required by the Web Form Designer
			//Do not modify it using the code editor.
			InitializeComponent();
		}
		
#endregion
		
		
#region Private Members
		
		
		private DotNetNuke.Entities.Modules.PortalModuleBase _loaderCtl;
		private FormSettings objFormSettings;
		private string strOperationSuccess;
		private ModuleInfo modInfo = null;
#endregion


		#region Event Handlers

		protected void Page_Load(System.Object sender, System.EventArgs e)
		{
			try
			{
				this.LoaderCtl = (PortalModuleBase)this.Parent.Parent;

				ModuleController modCtl = new ModuleController();
				modInfo = modCtl.GetModule(this.LoaderCtl.ModuleId);

				UserController objUsers = new UserController();
				UserInfo objUser = UserController.GetCurrentUserInfo();
				
				if (!(objUser.IsSuperUser || DotNetNuke.Security.PortalSecurity.IsInRole("Administrators")))
				{
					if (!DotNetNuke.Security.Permissions.ModulePermissionController.HasModuleAccess(DotNetNuke.Security.SecurityAccessLevel.Edit, "edit", modInfo))
						Response.Redirect("~/" + DotNetNuke.Common.Globals.glbDefaultPage);
				}
				
				strOperationSuccess = Localization.GetString("OperationSuccess", this.LocalResourceFile);
				objFormSettings = Utility.GetFormSettings(ModuleId);
				
				if (!Page.IsPostBack)
				{
					BindData();
				}
				
				if ((!Request.UserAgent.Contains("MSIE")) || Request.Browser.Platform.Contains("Mac"))
				{
					if (DotNetNuke.Framework.AJAX.IsInstalled())
					{
						Utility.DisableAjax(this);
					}
				}
				
			}
			catch (Exception exc)
			{
				Exceptions.ProcessModuleLoadException(LoaderCtl, exc);
			}
		}

		protected void cmdUpdate_Click(System.Object sender, System.EventArgs e)
		{
			
			try
			{
				objFormSettings.UserRoles = "";
				
				foreach (ListItem item in this.cblUserRoles.Items)
				{
					if (item.Selected)
					{
						objFormSettings.UserRoles += item.Text + ",";
					}
				}
				
				if (objFormSettings.UserRoles.Length > 0)
				{
					objFormSettings.UserRoles = objFormSettings.UserRoles.Substring(0, objFormSettings.UserRoles.Length - 1);
				}
				
				ModuleController objModules = new ModuleController();
				
				objFormSettings.RegisterUser = this.chkRegisterUser.Checked;
				objFormSettings.UserNameField = this.lstUserNameField.SelectedValue;
				objFormSettings.PasswordField = this.lstPasswordField.SelectedValue;
				objFormSettings.FirstNameField = this.lstFirstNameField.SelectedValue;
				objFormSettings.LastNameField = this.lstLastNameField.SelectedValue;
				objFormSettings.EmailField = this.lstEmailField.SelectedValue;
				
				objModules.UpdateModuleSetting(ModuleId, "UserRoles", objFormSettings.UserRoles);
				objModules.UpdateModuleSetting(ModuleId, "RegisterUser", objFormSettings.RegisterUser.ToString());
				objModules.UpdateModuleSetting(ModuleId, "UserNameField", objFormSettings.UserNameField);
				objModules.UpdateModuleSetting(ModuleId, "PasswordField", objFormSettings.PasswordField);
				objModules.UpdateModuleSetting(ModuleId, "FirstNameField", objFormSettings.FirstNameField);
				objModules.UpdateModuleSetting(ModuleId, "LastNameField", objFormSettings.LastNameField);
				objModules.UpdateModuleSetting(ModuleId, "EmailField", objFormSettings.EmailField);
				
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
				
			}
			catch (Exception exc)
			{
				Exceptions.ProcessModuleLoadException(LoaderCtl, exc);
			}
		}

		protected void chkRegisterUser_CheckedChanged(object sender, System.EventArgs e)
		{
			this.rfvUserNameField.Enabled = this.chkRegisterUser.Checked;
			this.rfvPasswordField.Enabled = this.chkRegisterUser.Checked;
			this.rfvFirstNameField.Enabled = this.chkRegisterUser.Checked;
			this.rfvLastNameField.Enabled = this.chkRegisterUser.Checked;
			this.rfvEmailField.Enabled = this.chkRegisterUser.Checked;
		}
#endregion
		
		
#region Data Bind Methods
		
		private void BindData()
		{
			
			Code5Systems.FormMaster.Business.FormController objFormCtl = new Code5Systems.FormMaster.Business.FormController(ModuleId);
			
			this.chkRegisterUser.Checked = objFormSettings.RegisterUser;
			
			objFormCtl.BindDynamicControls(this.lstUserNameField, ModuleId);
			this.lstUserNameField.Items.Insert(0, new ListItem(Localization.GetString("Select.Text", Utility.GetSharedResourceFile()), "-1"));
			this.lstUserNameField.SelectedIndex = this.lstUserNameField.Items.IndexOf(this.lstUserNameField.Items.FindByValue(objFormSettings.UserNameField));
			
			objFormCtl.BindDynamicControls(this.lstPasswordField, ModuleId);
			this.lstPasswordField.Items.Insert(0, new ListItem(Localization.GetString("Select.Text", Utility.GetSharedResourceFile()), "-1"));
			this.lstPasswordField.SelectedIndex = this.lstPasswordField.Items.IndexOf(this.lstPasswordField.Items.FindByValue(objFormSettings.PasswordField));
			
			objFormCtl.BindDynamicControls(this.lstFirstNameField, ModuleId);
			this.lstFirstNameField.Items.Insert(0, new ListItem(Localization.GetString("Select.Text", Utility.GetSharedResourceFile()), "-1"));
			this.lstFirstNameField.SelectedIndex = this.lstFirstNameField.Items.IndexOf(this.lstFirstNameField.Items.FindByValue(objFormSettings.FirstNameField));
			
			objFormCtl.BindDynamicControls(this.lstLastNameField, ModuleId);
			this.lstLastNameField.Items.Insert(0, new ListItem(Localization.GetString("Select.Text", Utility.GetSharedResourceFile()), "-1"));
			this.lstLastNameField.SelectedIndex = this.lstLastNameField.Items.IndexOf(this.lstLastNameField.Items.FindByValue(objFormSettings.LastNameField));
			
			objFormCtl.BindDynamicControls(this.lstEmailField, ModuleId);
			this.lstEmailField.Items.Insert(0, new ListItem(Localization.GetString("Select.Text", Utility.GetSharedResourceFile()), "-1"));
			this.lstEmailField.SelectedIndex = this.lstEmailField.Items.IndexOf(this.lstEmailField.Items.FindByValue(objFormSettings.EmailField));
			
			if (objFormSettings.RegisterUser)
			{
				this.rfvUserNameField.Enabled = true;
				this.rfvPasswordField.Enabled = true;
				this.rfvFirstNameField.Enabled = true;
				this.rfvLastNameField.Enabled = true;
				this.rfvEmailField.Enabled = true;
			}
			else
			{
				this.rfvUserNameField.Enabled = false;
				this.rfvPasswordField.Enabled = false;
				this.rfvFirstNameField.Enabled = false;
				this.rfvLastNameField.Enabled = false;
				this.rfvEmailField.Enabled = false;
			}
			
			DotNetNuke.Security.Roles.RoleController oRolesCtl = new DotNetNuke.Security.Roles.RoleController();
			
			this.cblUserRoles.DataSource = oRolesCtl.GetPortalRoles(PortalId);
			this.cblUserRoles.DataBind();
			
			for (int index = cblUserRoles.Items.Count - 1; index >= 0; index--)
			{
				if (cblUserRoles.Items[index].Value == "0" || cblUserRoles.Items[index].Value == "1")
				{
					cblUserRoles.Items.Remove(cblUserRoles.Items[index]);
				}
			}
			
			string[] aryRoles = null;
			
			if (objFormSettings.UserRoles != string.Empty)
			{
				aryRoles = objFormSettings.UserRoles.Split(',');
				
				for (int i = 0; i <= aryRoles.Length - 1; i++)
				{
					if (this.cblUserRoles.Items.FindByText(aryRoles[i]) != null)
					{
						this.cblUserRoles.Items.FindByText(aryRoles[i]).Selected = true;
					}
					
				}
			}
			
			
			
		}
		
#endregion
		
		
#region Public Properties
		public PortalModuleBase LoaderCtl
		{
			get
			{
				if (_loaderCtl == null)
				{
					_loaderCtl = this;
				}
				
				return _loaderCtl;
			}
			set
			{
				_loaderCtl = value;
			}
		}
#endregion

	}
	
}


